#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Git Hooks 动态配置系统 - 使用演示脚本
"""

import sys
import os
from pathlib import Path

# 添加项目根目录到 Python 路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from core.hook_manager import HookManager, HookType, HookContext

def demo_commit_msg_validation():
    """演示提交信息验证功能"""
    print("=" * 60)
    print("🔍 演示：提交信息验证功能")
    print("=" * 60)
    
    manager = HookManager()
    
    # 测试无效提交信息
    print("\n1️⃣ 测试无效提交信息:")
    print("   提交信息: 'bad commit message'")
    
    context = HookContext(hook_type=HookType.COMMIT_MSG)
    context.commit_msg = 'bad commit message'
    success = manager.execute_hook(HookType.COMMIT_MSG, context)
    
    print(f"   结果: {'✅ 通过' if success else '❌ 被拒绝 (正确行为)'}")
    
    # 测试有效提交信息
    print("\n2️⃣ 测试有效提交信息:")
    print("   提交信息: 'feat: add new feature'")
    
    context = HookContext(hook_type=HookType.COMMIT_MSG)
    context.commit_msg = 'feat: add new feature'
    success = manager.execute_hook(HookType.COMMIT_MSG, context)
    
    print(f"   结果: {'✅ 通过' if success else '❌ 被拒绝'}")

def demo_pre_commit_hook():
    """演示pre-commit Hook功能"""
    print("\n" + "=" * 60)
    print("🚀 演示：Pre-commit Hook 功能")
    print("=" * 60)
    
    manager = HookManager()
    
    print("\n执行 pre-commit 检查...")
    context = HookContext(hook_type=HookType.PRE_COMMIT)
    success = manager.execute_hook(HookType.PRE_COMMIT, context)
    
    print(f"结果: {'✅ 所有检查通过' if success else '❌ 检查失败'}")

def demo_config_management():
    """演示配置管理功能"""
    print("\n" + "=" * 60)
    print("⚙️ 演示：配置管理功能")
    print("=" * 60)
    
    # 显示当前配置
    print("\n📋 当前系统配置:")
    os.system(f"{sys.executable} tools/config.py show")

def demo_supported_commit_formats():
    """演示支持的提交信息格式"""
    print("\n" + "=" * 60)
    print("📝 支持的提交信息格式")
    print("=" * 60)
    
    formats = [
        ("feat: add new feature", "✅ 新功能"),
        ("fix: resolve bug in login", "✅ 修复bug"),
        ("docs: update README", "✅ 文档更新"),
        ("style: format code", "✅ 代码格式"),
        ("refactor: restructure auth module", "✅ 重构代码"),
        ("test: add unit tests", "✅ 添加测试"),
        ("chore: update dependencies", "✅ 维护任务"),
        ("feat(auth): add OAuth support", "✅ 带作用域的功能"),
        ("just a message", "❌ 格式错误"),
        ("fix", "❌ 描述太短"),
    ]
    
    manager = HookManager()
    
    for msg, expected in formats:
        context = HookContext(hook_type=HookType.COMMIT_MSG)
        context.commit_msg = msg
        success = manager.execute_hook(HookType.COMMIT_MSG, context)
        
        result = "✅ 通过" if success else "❌ 拒绝"
        print(f"   {msg:<35} → {result} {expected}")

def main():
    """主演示函数"""
    print("🎯 Git Hooks 动态配置系统 - 使用演示")
    print("=" * 60)
    
    try:
        # 1. 演示提交信息验证
        demo_commit_msg_validation()
        
        # 2. 演示pre-commit Hook
        demo_pre_commit_hook()
        
        # 3. 演示配置管理
        demo_config_management()
        
        # 4. 演示支持的提交格式
        demo_supported_commit_formats()
        
        print("\n" + "=" * 60)
        print("🎉 演示完成！")
        print("=" * 60)
        print("\n💡 使用提示:")
        print("1. 编辑 config/hooks.yaml 来自定义配置")
        print("2. 查看 .git/hooks-controller.log 了解详细日志")
        print("3. 使用 tools/config.py 管理配置")
        print("4. 现在可以正常使用 git commit 命令")
        
    except Exception as e:
        print(f"\n❌ 演示过程中出现错误: {e}")
        return False
    
    return True

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

